/**
 * 
 */
package org.monkeypuzzler.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;



public class SimpleHandler implements InvocationHandler
{
	Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
	
	public SimpleHandler(Interceptor[] interceptors) 
	{
		for (Interceptor in : interceptors)
			this.interceptors.put(in.getMethodName(), in);
	}
	
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable 
	{
		if (this.interceptors.containsKey(method.getName()))
			return this.interceptors.get(method.getName()).intercept(proxy, method, args);
		throw new AssertionError("no interceptor for method name ["+method.getName()+"]");
	}
	
}